<template>
  <div class="login">
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/logo.png" alt="login">
      </div>
      <el-form ref="loginInfoRef" :model="loginInfo" :rules="loginInfoRules" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
            <el-input v-model="loginInfo.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名" type="text"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
            <el-input v-model="loginInfo.password" prefix-icon="iconfont icon-3702mima" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      loginInfo: {
        username: '',
        password: ''
      },
      loginInfoRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 7, message: '长度在 3 到 7 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单内容和状态
    resetFrom: function () {
      // console.log(this)
      // 利用 模板对象的 refs 属性引用 ref 获取表单引用对象
      // this.$refs.loginInfoRef 表示表单的引用对象
      this.$refs.loginInfoRef.resetFields()
    },
    // 登录功能
    login () {
      // 表单预验证（提交前的验证）
      this.$refs.loginInfoRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginInfo)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登陆成功')
        // 登录之后将 token 值保存在 sessionStorage 中
        // 其他api需要登楼之后访问
        // token 只有当次登录该网站时使用
        window.sessionStorage.setItem('token', res.data.token)
        // 登陆成功跳转到 /home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  height: 100%;

  .login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 450px;
    height: 300px;
    border-radius: 10px;
    background-color: #ffffff;
    transform: translate(-50%, -50%);

    .avatar_box {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 130px;
      height: 130px;
      border: 1px solid #cccccc;
      border-radius: 50%;
      padding: 10px;
      background-color: #ffffff;
      box-shadow: 0 0 10px #cccccc;

      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #dddddd;
      }
    }

    .login_form{
      position: absolute;
      bottom: 0;
      box-sizing: border-box;
      padding: 0 20px;
      width: 100%;

      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
